package com.kkd.kkdapp.activity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.PaingCreditDetailAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.PaingCreditDetailData;
import com.kkd.kkdapp.http.entity.PaingCreditDetailList;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.view.CustomScrollView;
import com.kkd.kkdapp.view.CustomScrollView.OnRefreshListener;
import com.kkd.kkdapp.view.ForScrollListView;

/**
 * 在借详情
 * 
 * @author mty
 * 
 */
public class PaingCreditDetailActivity extends BaseActivity implements
		OnClickListener {

	private final int NOCODE_ZERO = 1; // 请求code不等于0 Toast
	private final int CODE_ZERO = 2; // 请求code等于0
	private ForScrollListView mListView;
	private RelativeLayout condition_img; // 后退
	private List<PaingCreditDetailList> mList;
	private List<PaingCreditDetailList> bills;
	private String desc;
	private PaingCreditDetailAdapter adapter;
	private int unpay_number; // 剩余期数
	private TextView unpay_number_tv;
	private TextView amount_tv; // 借款总额
	private DecimalFormat df1 = new DecimalFormat("###,##0.00");
	private DecimalFormat df = new DecimalFormat("0.00");
	private TextView lending_rate; // 利率
	private TextView terminally; // 应还金额
	private TextView periods_tv; // 分期期数
	private String bidId;
	private CustomScrollView scrollview_paing;
	private Dialog mDialog;
	private RelativeLayout howrepayment; // 如何还款
	private String status;
	private ImageView paiddebt_img; // 已还清图片
	private LinearLayout repayment_ll; // 还款总额
	private long totalPayMoney; // 应还总额
	private TextView repayment_tv;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case NOCODE_ZERO:
				showToast(desc);
				break;
			case CODE_ZERO:
				if (unpay_number == 0) {
					paiddebt_img.setVisibility(View.VISIBLE);
					repayment_ll.setVisibility(View.VISIBLE);
					repayment_tv.setText("" + df1.format(totalPayMoney * 0.01));
					unpay_number_tv.setText("还款记录");
					MyCreditActivity.flag_bidId_changeToPayOff = bidId;
				} else {
					paiddebt_img.setVisibility(View.GONE);
					repayment_ll.setVisibility(View.GONE);
					unpay_number_tv.setText("还款记录（剩余" + unpay_number + "期）");
					MyCreditActivity.flag_bidId_changeToPayOff = "";
				}
				if (mList != null && mList.size() > 0) {
					mListView.setVisibility(View.VISIBLE);
					adapter.notifyDataSetChanged();
				} else {
					mListView.setVisibility(View.GONE);
				}
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.paing_credit_detail_activity);
		initView();
		setListener();
		getMessage();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.kkd.kkdapp.base.BaseActivity#onResume()
	 */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (!TextUtils.isEmpty(bidId)) {
			questBillList();
		}
	}

	public void initView() {
		scrollview_paing = (CustomScrollView) this
				.findViewById(R.id.scrollview_paing);
		condition_img = (RelativeLayout) this.findViewById(R.id.condition_img);
		mListView = (ForScrollListView) this.findViewById(R.id.listview);
		unpay_number_tv = (TextView) this.findViewById(R.id.unpay_number_tv);
		amount_tv = (TextView) this.findViewById(R.id.amount_tv);
		mListView.setFocusable(false);
		lending_rate = (TextView) this.findViewById(R.id.lending_rate);
		terminally = (TextView) this.findViewById(R.id.terminally);
		periods_tv = (TextView) this.findViewById(R.id.periods);
		howrepayment = (RelativeLayout) this.findViewById(R.id.howrepayment);
		paiddebt_img = (ImageView) this.findViewById(R.id.paiddebt_img);
		repayment_ll = (LinearLayout) this.findViewById(R.id.repayment_ll);
		repayment_tv = (TextView) this.findViewById(R.id.repayment_tv);
		if (mList == null) {
			mList = new ArrayList<PaingCreditDetailList>();
		}
		adapter = new PaingCreditDetailAdapter(this, mList);
		mListView.setAdapter(adapter);
	}

	public void setListener() {
		condition_img.setOnClickListener(this);
		howrepayment.setOnClickListener(this);
		scrollview_paing.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				mHandler.postDelayed(new Runnable() {

					@Override
					public void run() {
						getMessage();
						scrollview_paing.onRefreshComplete();
					}
				}, 1000);
			}
		});
	}

	@SuppressLint("SimpleDateFormat")
	public void getMessage() {
		Intent intent = this.getIntent();
		long amount = intent.getLongExtra("amount", -1);
		if (amount != -1) {
			amount_tv.setText("" + df1.format(amount * 0.01));
		}
		long totalPayAmount = intent.getLongExtra("totalPayAmount", -1);
		if (totalPayAmount != -1) {
			lending_rate.setText(df1.format(totalPayAmount * 0.01));
		}
		long currentPeriodAmount = intent.getLongExtra("currentPeriodAmount",
				-1);
		if (currentPeriodAmount != -1) {
			terminally.setText(df1.format(currentPeriodAmount * 0.01));
		}
		int periods = intent.getIntExtra("periods", 0);
		if (periods != 0) {
			periods_tv.setText(periods + "");
		}
		bidId = intent.getStringExtra("bidId");
		LogUtil.e("bidId", bidId);
		status = intent.getStringExtra("status");
		if (TextUtils.isEmpty(status)) {
			if ("5005".equals(status)) { // 已还清
				paiddebt_img.setVisibility(View.VISIBLE);
				repayment_ll.setVisibility(View.VISIBLE);
			} else if ("5004".equals(status)) { // 未还清
				paiddebt_img.setVisibility(View.GONE);
				repayment_ll.setVisibility(View.GONE);
			}
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.condition_img:
			finish();
			break;
		case R.id.howrepayment:
			getHowRepayment();
			break;
		}
	}

	/**
	 * 在借详情接口
	 * 
	 */
	public void questBillList() {
		LogUtil.e("bidId", bidId);
		KkdHttpMagager.getInstance().getBillList(bidId,
				PaingCreditDetailData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						PaingCreditDetailData data = (PaingCreditDetailData) responseBody;
						unpay_number = data.getUnpay_number();
						totalPayMoney = data.getTotalPayMoney();
						bills = data.getBills();
						mList.clear();
						mList.addAll(bills);
						mHandler.sendEmptyMessage(2);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						PaingCreditDetailActivity.this.desc = desc;
						mHandler.sendEmptyMessage(1);
					}
				});
	}

	// 如何还款
	public void getHowRepayment() {
		if (mDialog != null) {
			mDialog.cancel();
			mDialog = null;
		}
		mDialog = DialogUtils.howRepayment(this, new OnClickListener() {

			@Override
			public void onClick(View v) {
				mDialog.dismiss();
			}
		});
	}
}
